package com.limh.jkzs.fragment

import android.support.v4.view.ViewPager
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.RadioGroup
import android.widget.TextView
import com.limh.blehelper.Ble2Manager
import com.limh.blehelper.event.ConnectEvent
import com.limh.jkzs.R
import com.limh.jkzs.activity.ScanActivity
import com.limh.jkzs.adapter.FragmentAdapter
import com.limh.jkzs.base.BaseFragment
import com.limh.jkzs.fragment.blue.ChartFragment
import com.limh.jkzs.fragment.blue.ControlFragment
import com.limh.jkzs.fragment.blue.SerialFragment
import com.limh.jkzs.util.Logs
import com.limh.jkzs.widget.ParentViewPager
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Function:
 * author: limh
 * time:2018/1/17
 */
class BlueFragment : BaseFragment(), View.OnClickListener {
    private lateinit var vpBlue: ParentViewPager
    private lateinit var radioBlue: RadioGroup
    private lateinit var imageStatus: ImageView
    private lateinit var txtAddress: TextView

    private val radioId = mutableListOf(R.id.radio_1, R.id.radio_2, R.id.radio_3)

    private val fragments = mutableListOf<BaseFragment>()
    private var adapter: FragmentAdapter? = null

    override fun findView(): Int {
        return R.layout.fragment_blue
    }

    override fun initView(view: View) {
        vpBlue = view.findViewById(R.id.vpBlue)
        radioBlue = view.findViewById(R.id.radioBlue)
        imageStatus = view.findViewById(R.id.imageStatus)
        txtAddress = view.findViewById(R.id.txtAddress)

        imageStatus.setOnClickListener(this)

        fragments.add(SerialFragment())
        fragments.add(ControlFragment())
        fragments.add(ChartFragment())
        adapter = FragmentAdapter(childFragmentManager, fragments)
        vpBlue.adapter = adapter
        vpBlue.currentItem = 0
        vpBlue.offscreenPageLimit = 3
        vpBlue.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
                if (state == 2) {
                    radioBlue.check(radioId[vpBlue.currentItem])
                }
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {

            }

        })

        radioBlue.setOnCheckedChangeListener { _, checkId ->
            when (checkId) {
                radioId[0] -> vpBlue.setCurrentItem(0, true)
                radioId[1] -> vpBlue.setCurrentItem(1, true)
                radioId[2] -> vpBlue.setCurrentItem(2, true)
            }
        }

        EventBus.getDefault().register(this)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun onConnStatus(connectEvent: ConnectEvent) {
        when (connectEvent.connStatus) {
            ConnectEvent.CONN_START -> {

            }
            ConnectEvent.CONN_SUCCESS -> {
                Logs.d(TAG, "name=" + connectEvent.device.name)
                Logs.d(TAG, "advice=" + connectEvent.device.address)
                imageStatus.setImageResource(R.drawable.ic_bluetooth_connected_white)
                if (TextUtils.isEmpty(connectEvent.device!!.name)) {
                    txtAddress.text = String.format("已连接：%s", connectEvent.device!!.address)
                } else {
                    txtAddress.text = String.format("已连接：%s", connectEvent.device!!.name)
                }
            }
            ConnectEvent.CONN_FAIL -> {

            }
            ConnectEvent.CONN_DISCONN -> {
                txtAddress.text = "连接已断开"
                imageStatus.setImageResource(R.drawable.ic_bluetooth_disabled_white)
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.imageStatus -> {
                if (!Ble2Manager.getInstance().isSupportBle) {
                    showShort("该设备不支持蓝牙")
                    return
                }
                if (!Ble2Manager.getInstance().isBleEnable) {
                    Ble2Manager.getInstance().openBle()
                }
                openActivity(ScanActivity::class.java)
            }
        }
    }
}